package com.thinkwide.data.util;

import com.thinkwide.data.ormapping.db.MFDbType;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 数据库判断
 *
 * @author andyzhang
 * @since 2023/06/07
 */
public class DbUtils {
    public static MFDbType getDataBaseType(Connection connection) throws SQLException {

        if (connection.getMetaData().getDriverName().toLowerCase().indexOf("mysql") != -1) {
            return MFDbType.MYSQL;
        } else if (connection.getMetaData().getDriverName().toLowerCase().indexOf("sqlserver") != -1) {
            return MFDbType.MSSQL;
        } else if (connection.getMetaData().getDriverName().toLowerCase().indexOf("oracle") != -1) {
            return MFDbType.ORACLE;
        } else if (connection.getMetaData().getDriverName().toLowerCase().indexOf("db2") != -1) {
            return MFDbType.DB2;
        } else if (connection.getMetaData().getDriverName().toLowerCase().indexOf("postgresql") != -1) {
            return MFDbType.POSTGRESQL;
        } else if (connection.getMetaData().getDriverName().toLowerCase().indexOf("oscar") != -1) {
            return MFDbType.OSCAR;
        } else if (connection.getMetaData().getDriverName().toLowerCase().indexOf("dm") != -1) {
            return MFDbType.DM;
        } else if (connection.getMetaData().getDriverName().toLowerCase().indexOf("gbase") != -1) {
            return MFDbType.GBASE;
        } else if (connection.getMetaData().getDriverName().toLowerCase().indexOf("kingbase") != -1) {
            return MFDbType.KINGBASE;
        }
        return MFDbType.MYSQL;
    }

    public static MFDbType getDataBaseType(String driverClassName) {
        if (driverClassName.toLowerCase().indexOf("mysql") != -1) {
            return MFDbType.MYSQL;
        } else if (driverClassName.toLowerCase().indexOf("sqlserver") != -1) {
            return MFDbType.MSSQL;
        } else if (driverClassName.toLowerCase().indexOf("oracle") != -1) {
            return MFDbType.ORACLE;
        } else if (driverClassName.toLowerCase().indexOf("db2") != -1) {
            return MFDbType.DB2;
        } else if (driverClassName.toLowerCase().indexOf("postgresql") != -1) {
            return MFDbType.POSTGRESQL;
        } else if (driverClassName.toLowerCase().indexOf("oscar") != -1) {
            return MFDbType.OSCAR;
        } else if (driverClassName.toLowerCase().indexOf("dm") != -1) {
            return MFDbType.DM;
        } else if (driverClassName.toLowerCase().indexOf("gbase") != -1) {
            return MFDbType.GBASE;
        } else if (driverClassName.toLowerCase().indexOf("kingbase") != -1) {
            return MFDbType.KINGBASE;
        }
        return MFDbType.MYSQL;
    }

    public static String getAdapterClasses(MFDbType dbType) {
        switch (dbType) {
            case MYSQL:
                return "com.thinkwide.mf.ormapping.sql.MFMySql";
            case MSSQL:
                return "com.thinkwide.mf.ormapping.sql.MFMsSql";
            case ORACLE:
                return "com.thinkwide.mf.ormapping.sql.MFOracleSql";
            case DB2:
                return "com.thinkwide.mf.ormapping.sql.MFDb2Sql";
            case POSTGRESQL:
                return "com.thinkwide.mf.ormapping.sql.MFPostgerSql";
            case OSCAR:
                return "com.thinkwide.mf.ormapping.sql.MFOracleSql";
            case DM:
                return "com.thinkwide.mf.ormapping.sql.MFOracleSql";
            case GBASE:
                return "com.thinkwide.mf.ormapping.sql.MFOracleSql";
            case KINGBASE:
                return "com.thinkwide.mf.ormapping.sql.MFOracleSql";

            default:
                return "com.thinkwide.mf.ormapping.sql.MFMySql";
        }
    }

}
